﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.ClientEditor
{

	/// <summary>
	/// Declaration of InvoiceSettingsCtrl class
	/// Control for editing invoice settings, this control is the first tab
	/// in the bank accounts tab control
	/// </summary>
	public partial class InvoiceSettingsCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		// List of combo-boxes that display contacts for this client
		private List<ComboBox> m_contactComboBoxes;

		/////////////////////////////////////////////////////////////
		// Construction

		public InvoiceSettingsCtrl()
		{

			// Load xaml
			InitializeComponent();

			// Retain a list of combo boxes that display contacts
			m_contactComboBoxes = new List<ComboBox>();
			m_contactComboBoxes.Add(cboInvoiceContact);
			_reloadContacts(null, null);

			// Set controls - cboInvoicingFrequency
			ComboBoxObject.LoadFromEnumeration(cboInvoicingFrequency,typeof(Definitions.eClientInvoicingFrequency));
			cboInvoicingFrequency.SelectedIndex = 0;

			// Set controls - cboInvoiceDelivery
			cboInvoiceDelivery.Items.Add(new ComboBoxObject(Definitions.NotSet, 0, null));
			cboInvoiceDelivery.Items.Add(new ComboBoxObject("Email", (int)Definitions.eClientInvoiceStatementTravel.Email, null ));
			cboInvoiceDelivery.Items.Add(new ComboBoxObject("Post", (int)Definitions.eClientInvoiceStatementTravel.Post, null ));
			cboInvoiceDelivery.Items.Add(new ComboBoxObject("Post + Email", (int)(Definitions.eClientInvoiceStatementTravel.Post|Definitions.eClientInvoiceStatementTravel.Email), null ));
			cboInvoiceDelivery.SelectedIndex = 0;

			// Set controls - cboStatementFrequency
			ComboBoxObject.LoadFromEnumeration(cboStatementFrequency, typeof(Definitions.eClientStatementFrequency));
			cboStatementFrequency.SelectedIndex = 0;

			// Set controls - cboStatementDelivery
			cboStatementDelivery.Items.Add(new ComboBoxObject(Definitions.NotSet, 0, null));
			cboStatementDelivery.Items.Add(new ComboBoxObject("Email", (int)Definitions.eClientInvoiceStatementTravel.Email, null));
			cboStatementDelivery.Items.Add(new ComboBoxObject("Post", (int)Definitions.eClientInvoiceStatementTravel.Post, null));
			cboStatementDelivery.Items.Add(new ComboBoxObject("Post + Email", (int)(Definitions.eClientInvoiceStatementTravel.Post | Definitions.eClientInvoiceStatementTravel.Email), null));
			cboStatementDelivery.SelectedIndex = 0;

			// Set controls - cboInvoicingCurrency
			Money.LoadCurrencyControl(cboInvoicingCurrency, true);
			cboInvoicingCurrency.SelectedIndex = 0;

			// Set controls - cboClientFundsSettings
			ComboBoxObject.LoadFromEnumeration(cboClientFundsSettings, typeof(Definitions.eClientFundsSettings));
			cboClientFundsSettings.SelectedIndex = 0;

			// Set controls - cboClientAccountStatus
			ComboBoxObject.LoadFromEnumeration(cboClientAccountStatus, typeof(Definitions.eClientAccountStatus));
			cboClientAccountStatus.SelectedIndex = 0;

			// Set controls - cboClientFundsFrequency
			ComboBoxObject.LoadFromEnumeration(cboClientFundsFrequency, typeof(Definitions.eClientFundsFrequency));
			cboClientFundsFrequency.SelectedIndex = 0;

			// Set controls - cboPaymentMethod
			ComboBoxObject.LoadFromEnumeration(cboPaymentMethod, typeof(Definitions.eClientPaymentMethod));
			cboPaymentMethod.SelectedIndex = 0;

		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{

			// If not in design mode...
			if (
				(App.Current != null)
				&&
				(App.Current.RootVisual != null)
				&&
				((App.Current.RootVisual as MainPage) != null)
			)
			{

				// Need to know when the list of contacts has changed, so combo-boxes can be reloaded
                (App.Current.RootVisual as MainPage).ClientEditor.tabContacts.ContactListChange += new EventHandler(TabContacts_ContactListChange);

			}

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		void TabContacts_ContactListChange(object sender, EventArgs e)
		{

			// Get the contacts map
			Dictionary<int, string> contactsMap = sender as Dictionary<int, string>;

			// Reload contacts
			_reloadContacts(contactsMap, null);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _reloadContacts(Dictionary<int, string> contactsMap, ComboBox Ignore)
		{

			// Loop through combo boxes
			foreach (ComboBox ctrl in m_contactComboBoxes)
			{

				// Get the selected item
				int iSelectedItemData = 0;
				if (ctrl.SelectedItem != null)
					iSelectedItemData = (ctrl.SelectedItem as ComboBoxObject).ItemData;

				// Load the combo boxes
				ctrl.Items.Clear();
				ctrl.Items.Add(new ComboBoxObject(Definitions.NotSet, 0, null));
				if (contactsMap != null)
				{
					foreach (var di in contactsMap)
					{
						ComboBoxObject newCbo = new ComboBoxObject(di.Value, di.Key, null);
						ctrl.Items.Add(newCbo);
					}
				}

				// Re-select contact
				if (ComboBoxObject.ContainsByItemData(ctrl, iSelectedItemData))
					ComboBoxObject.SelectByItemData(ctrl, iSelectedItemData);
				else
					ctrl.SelectedIndex = 0;

			}

		}

		public void PostSupportDataLoaded()
		{

			// Called after plug-in has loaded support data

		}

		public void Set( ClientTransport clientTransport )
		{

			// Set controls
			ComboBoxObject.SelectByItemData(cboInvoicingFrequency, clientTransport.InvoicingFrequency);
			ComboBoxObject.SelectByItemData(cboInvoiceDelivery, clientTransport.InvoiceTravel);
			ComboBoxObject.SelectByItemData(cboInvoiceContact, clientTransport.InvoiceContactId);
			ComboBoxObject.SelectByItemData(cboStatementFrequency, clientTransport.StatementFrequency);
			ComboBoxObject.SelectByItemData(cboStatementDelivery, clientTransport.StatementTravel);
			txtVATNumber.Text = clientTransport.VATNumber ?? string.Empty;
			ComboBoxObject.SelectByTagString(cboInvoicingCurrency, clientTransport.InvoicingCurrency);
			ComboBoxObject.SelectByItemData(cboClientFundsSettings, clientTransport.ClientFundsSettings);
			ComboBoxObject.SelectByItemData(cboClientAccountStatus, clientTransport.ClientAccountStatus);
			ComboBoxObject.SelectByItemData(cboClientFundsFrequency, clientTransport.ClientFundsFrequency);
			ComboBoxObject.SelectByItemData(cboPaymentMethod, clientTransport.PaymentMethod);

		}

		public void Get(ClientTransport clientTransport, List<string> Errors)
		{

			// Populate the client object with details from the control
			// and find errors in the user input

			// Get values from controls
			clientTransport.InvoicingFrequency = (cboInvoicingFrequency.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.InvoiceTravel = (cboInvoiceDelivery.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.InvoiceContactId = (cboInvoiceContact.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.StatementFrequency = (cboStatementFrequency.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.StatementTravel = (cboStatementDelivery.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.VATNumber = txtVATNumber.Text;

			// Add currency
			string strCurrency = null;
			object oTag = (cboInvoicingCurrency.SelectedValue as ComboBoxObject).Tag;
			if (oTag != null)
				strCurrency = oTag.ToString();
			clientTransport.InvoicingCurrency = strCurrency;

			clientTransport.ClientFundsSettings = (cboClientFundsSettings.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.ClientAccountStatus = (cboClientAccountStatus.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.ClientFundsFrequency = (cboClientFundsFrequency.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.PaymentMethod = (cboPaymentMethod.SelectedValue as ComboBoxObject).ItemData;

		}

	}

}
